/*
 * Copyright 2016-2018 TVI Go Easy.
 * Created on 2018/1/23 17:02
 */
package com.tvi.cloud.license.controller;

import com.tvi.cloud.license.model.License;
import com.tvi.cloud.license.service.LicenseService;
import com.tvi.cloud.license.service.OrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 请在此写下该类的说明
 *
 * @author mechanic
 */
//表示该类中的变量会被刷新，该注解经测试放在启动文件中不起作用。
@RefreshScope
@RestController
@RequestMapping(path = "v1/license")
@Slf4j
public class LicenseController {
    @Value("${example.property}")
    private String exampleProperty;

    @Autowired
    private LicenseService licenseService;

    @Autowired
    private OrganizationService organizationService;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public License get(@PathVariable("id") String id) {
        log.debug("Entering the get() method for the licenseId: {}", id);
        return licenseService.findOne(id);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public License save(@RequestBody License license) {
        return this.licenseService.save(license);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void delete(@PathVariable("id") String id) {
        this.licenseService.delete(id);
    }

    @RequestMapping(value = "/example-property", method = RequestMethod.GET)
    public String exampleProperty() {
        return getExampleProperty();
    }

    @RequestMapping(value = "/org/{id}", method = RequestMethod.GET)
    public Object getOrg(@PathVariable("id") String id, @RequestParam String clientType) {
        return organizationService.retrieveOrgInfo(id, clientType);
    }

    @RequestMapping(value = "/getLicenseByOrganizationId", method = RequestMethod.GET)
    public List<License> getLicenseByOrganizationId(@RequestParam("organizationId") String organizationId) {
        return licenseService.getLicenseList(organizationId);
    }

    private String getExampleProperty() {
        return exampleProperty;
    }

    public void setExampleProperty(String exampleProperty) {
        this.exampleProperty = exampleProperty;
    }
}
